/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-26 上午10:22 Created by dwzhang
 */
package com.jade.framework.startup;

import java.util.HashSet;
import java.util.Set;

/**
 * 程序启动管理器。
 *
 * @author dwzhang
 * @version 1.0 13-4-26 上午10:22
 */
public class Launcher
{
    private static ClassLoader loader;

    /** 主线程，在系统出始化的时候有效 */
    private static Thread mainThread = null;

    /** Server 对象 */
    private static Server server;

    private static final Set<String> ACTIONS = new HashSet<String>();

    static {
        ACTIONS.add("start");
        ACTIONS.add("stop");
        ACTIONS.add("status");
    }

    public static void main(final String[] args)
    {
        //init();
        if (args.length == 1) {
            String conf = args[0];
            if (conf.endsWith(".xml")) {
                //初始化所有的Jar
                //initClasspathMeta();
                //beforeServiceMetaLoading();
                //loadServiceMeta(conf);
                //afterServiceMetaLoaded();
                doStart(args[0]);
            }
            else {
                System.out.println(conf + " couldn't be a valid config file");
                invalidCommand();
            }
        }
        else {
            if (ACTIONS.contains(args[0])) {
                //属于有命令的
                if (args.length >= 3) {
                    String action = args[0];
                    if ("start".equals(action)) {
                        //if (args.length == 4) {
                        //    System.setProperty("os.host", args[3]);
                        //    System.setProperty("cube.system.host", args[3]);
                        //    Jade.setProperty("cube.system.host", args[3]);
                        //}
                        //初始化所有的Jar
                        //initClasspathMeta();
                        doStart(args[1], args[2]);
                    }
                    else if ("stop".equals(action)) {
                        //初始化naisa-base.jar
                        //initClasspathMeta(NAISA_BASE, false);
                        //initClasspathMeta(CENO_BASE);
                        //String hostName = args.length == 4 ? args[3] : getHostName();
                        //doStatusOrStop("stop".equals(action), args[1], args[2], hostName);
                        if (server != null) {
                            server.stop();
                        }
                        else {
                            //todo 采用socket的方式去通知
                        }
                    }
                    else if ("status".equals(action)) {
                        if (server != null) {
                            System.out.println("Server " + args[2] + " is " +
                                               (server.isStarted() ? " started!" : " not started!"));
                        }
                        else {
                            //todo 采用socket的方式去获取状态
                        }
                    }
                    else {
                        System.out.println("No such action:" + action);
                        invalidCommand();
                    }
                }
                else {
                    invalidCommand();
                }
            }
        }
    }

    public static boolean isLaunched()
    {
        if (server == null) {
            return false;
        }
        else {
            return server.isStarted();
        }
    }

    protected static void doStart(String configXml)
    {
        server = new Server("default", configXml, true);
        server.start();
    }

    protected static void doStart(String serversXml, String type)
    {
        server = new Server(type, serversXml, false);
        server.start();
    }

    protected static void invalidCommand()
    {
        System.out.println("Usage: java com.jade.framework.startup.Launcher [options]");
        System.out.println("Options:");
        System.out.println(" xml-config-file                     ---xml Config file");
        System.out.println(" start xml-config-file type          ---Start a 'type' server");
        System.out.println(" stop xml-config-file type           ---Stop a 'type' server");
        System.out.println(" status xml-config-file type         ---Get status from a 'type' server on host");
        System.exit(0);
    }
}
